package view.Core;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.*;

import java.io.*;

import org.apache.*;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.*;

public class treeDataUpdate extends Activity {
	String treeID = "T0001";
	private static final int REQUEST_CODE = 3434;
	EditText updateTreeID, updateDBH, updateHeight, updateCrownSpread,
			updateEstimatedAge, updateLocation, updateLongitude,
			updateLatitude;
	
	ImageButton treeDataUpdateimageButton;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.update_display);

		findViewByID();
		//setText();
		selectAllFromDB();

	}

	public void OnClicktreeDataUpdateimageButton(View view){
		Intent intent = new Intent();
		Intent i = new Intent(this, encodeActivitySub.class);
		startActivityForResult(i, REQUEST_CODE);
	}
	
	public void selectAllFromDB() {
		String reply ;
		try {
			// String url =
			// "http://192.168.1.101/oldTreeWeb/getJsonField.php?Name=Tree tat";

			String url;
			
			//String inputTreeID = "inputTreeID=" + "T0001";
			url =  getResources().getString(R.string.serverURL)+"getJsonByTreeID.php?inputTreeID="+treeID;
			/*
			 * String url = getResources().getString(R.string.serverURL)
			 * +getResources().getString(R.string.ByTreeIDPHP) +inputTreeID;
			 */
			/*
			url = getResources().getString(R.string.serverURL)
					+ "getJsonByQR.php?" + inputTreeID;*/
			// getJsonByTreeID.php
			HttpClient client = new DefaultHttpClient();
			HttpGet request = new HttpGet(url);
			HttpResponse response = client.execute(request);

			String line = "";
			reply = "";
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					response.getEntity().getContent()));
			while ((line = reader.readLine()) != null) {
				reply = line;
			}
			reader.close();
			makeToast(reply);
			
		} catch (Exception e) {
			makeToast(""+e);
		}

	}

	
	
	
public void makeToast(String toast){
	Toast.makeText(this,toast,Toast.LENGTH_LONG).show();
	
}
	
	public void setText() {
		updateTreeID.setText("Good");
		updateDBH.setText("Good");
		updateHeight.setText("Good");
		updateCrownSpread.setText("Good");
		updateEstimatedAge.setText("Good");
		updateLocation.setText("Good");
		updateLongitude.setText("Good");
		updateLatitude.setText("Good");

	}

	public void findViewByID() {
		updateTreeID = (EditText) findViewById(R.id.updateTreeID);
		updateDBH = (EditText) findViewById(R.id.updateDBH);
		updateHeight = (EditText) findViewById(R.id.updateHeight);
		updateCrownSpread = (EditText) findViewById(R.id.updateCrownSpread);
		updateEstimatedAge = (EditText) findViewById(R.id.updateEstimatedAge);
		updateLocation = (EditText) findViewById(R.id.updateLocation);
		updateLongitude = (EditText) findViewById(R.id.updateLongitude);
		updateLatitude = (EditText) findViewById(R.id.updateLatitude);
		treeDataUpdateimageButton = (ImageButton)findViewById(R.id.treeDataUpdateimageButton);
	}
	
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == RESULT_OK && requestCode == REQUEST_CODE) {
			if (data.hasExtra("decodeString")) {
				String decodeString = data.getExtras()
						.getString("decodeString");
				// findtree_TV.setText(decodeString);
				updateTreeID.setText(decodeString);
				Toast.makeText(this, decodeString, Toast.LENGTH_LONG).show();
				// /String playTime =
				// data.getExtras().getString("decodeString");

			}
		}
	}
}
